//
//  MultiSelectController.m
//  CommonProject
//
//  Created by 郝旭珊 on 2017/12/28.
//  Copyright © 2017年 郝旭珊. All rights reserved.
//

#import "MultiSelectController.h"
#import "MessageCell.h"


@interface MultiSelectController()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *messageList;
@property (nonatomic, strong) UIView *footerView;


@end

@implementation MultiSelectController

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.backgroundColor = GRAYCOLOR_BACKGROUND;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"消息";
    [self setupDeleteView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.footerView];
    [self.view bringSubviewToFront:self.footerView];
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.rowHeight = 90;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarItemClick:)];
    self.navigationItem.rightBarButtonItem = item;
    
}


- (void)setupDeleteView{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-BUTTON_HEIGHT-NAVGATION_MAXY, SCREEN_WIDTH, BUTTON_HEIGHT)];
    footerView.backgroundColor = WHITECOLOR;
    footerView.hidden = YES;
    self.footerView = footerView;
    
    UIButton *selectAllButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, footerView.height)];
    selectAllButton.titleLabel.font = FONT_NORMAL;
    [selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
    [selectAllButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
    [selectAllButton setImage:[UIImage imageNamed:@"circle_empty"] forState:UIControlStateNormal];
    [selectAllButton setImage:[UIImage imageNamed:@"circle_tick_orange"] forState:UIControlStateSelected];
    selectAllButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [selectAllButton addTarget:self action:@selector(selectAll:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90, 0, 90, footerView.height)];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    deleteButton.titleLabel.font = FONT_NORMAL;
    deleteButton.backgroundColor = MAJORCOLOR;
    
    [footerView addSubview:selectAllButton];
    [footerView addSubview:deleteButton];
}


- (void)selectAll:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected){
        [self.tableView.visibleCells enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
        }];
        [sender setTitle:@"全不选" forState:UIControlStateNormal];
    }else{
        [self.tableView reloadData];
        /** 遍历反选
         [[self.tableView indexPathsForSelectedRows] enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         [self.tableView deselectRowAtIndexPath:obj animated:NO];
         }];
         */
        [sender setTitle:@"全选" forState:UIControlStateNormal];
    }
}


- (void)rightBarItemClick:(UIBarButtonItem *)item{
    if ([item.title isEqualToString:@"编辑"]) {
        //        if (self.messageList.count == 0) {
        //            return;
        //        }
        item.title = @"完成";
        [self.tableView setEditing:YES animated:YES];
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, BUTTON_HEIGHT, 0);
        self.footerView.hidden = NO;
    }else{
        item.title = @"编辑";
        [self.tableView setEditing:NO animated:YES];
        self.tableView.contentInset = UIEdgeInsetsZero;
        self.footerView.hidden = YES;
    }
    
}


- (void)showNoMessage{
    UIButton *noMessageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 100, 150)];
    [noMessageButton setTitle:@"还没有消息内容" forState:UIControlStateNormal];
    [noMessageButton setImage:[UIImage imageNamed:@"message"] forState:UIControlStateNormal];
    [noMessageButton sizeToFit];
    [noMessageButton layoutButtonWithImagePosition:ButtonImagePositionTop imageTitleSpace:15];
    noMessageButton.titleLabel.font = FONT_NORMAL;
    [noMessageButton setTitleColor:GRAYCOLOR_TEXT_LIGHT forState:UIControlStateNormal];
    noMessageButton.centerX = self.view.centerX;
    noMessageButton.userInteractionEnabled = NO;
    [self.view addSubview:noMessageButton];
    [self.view bringSubviewToFront:noMessageButton];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - UITabelViewDataSource

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil){
        cell = [[MessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell assignData];
    return cell;
}



@end
